﻿using LxBasic;
using System;

namespace LxCore
{
    public class BoussinesqSource : FieldBase<Vector3d>
    {
        public BoussinesqSource(IField<double> RefT, IField<double> ThermExpanCoeff, IField<double> Density, IField<Vector3d> Gravity, FvZone zone)
        {
            this.RefT = RefT;
            this.ThermExpanCoeff = ThermExpanCoeff;
            this.Density = Density;
            this.Gravity = Gravity;
        }
        protected IField<double> RefT;
        protected IField<double> ThermExpanCoeff;
        protected IField<double> Density;
        protected IField<Vector3d> Gravity;
        public override ICellVarArr<Vector3d> GetCellVarArr(IIndexHost indexHost)
        {
            FvZone zone = (indexHost as FvInner).InZone;
            if (RefT is ConstField.Double CRefT && ThermExpanCoeff is ConstField.Double CThermExpandCoeff
                && Density is ConstField.Double CRefDensity && Gravity is ConstField.Vector CGravity)
            {
                return new BoussinesqSourceVars_Const()
                {
                    ScalarT = ResHelper.GetField(zone).Temperature.InnX,
                    RefT = CRefT.GetPara(),
                    Total = CRefDensity.GetPara() * CThermExpandCoeff.GetPara() * CGravity.GetPara()
                };
            }
            else
            {
                return new BoussinesqSourceVars()
                {
                    ScalarT = ResHelper.GetField(zone).Temperature.InnX,
                    RefT = RefT.GetCellVarArr(indexHost),
                    RefDensity = Density.GetCellVarArr(indexHost),
                    ThermExpanCoeff = ThermExpanCoeff.GetCellVarArr(indexHost),
                    Gravity = Gravity.GetCellVarArr(indexHost),
                    //Total = RefDensity.GetPara() * ThermExpanCoeff.GetPara() * Gravity.GetPara()
                };
            }
        }
        protected class BoussinesqSourceVars : ICellVarArr<Vector3d>
        {
            public double[] ScalarT;
            public ICellVarArr<double> RefT;
            public ICellVarArr<double> ThermExpanCoeff;
            public ICellVarArr<double> RefDensity;
            public ICellVarArr<Vector3d> Gravity;
            public Vector3d GetCellVar(int cellID)
            {
                return (RefT.GetCellVar(cellID) - ScalarT[cellID]) * RefDensity.GetCellVar(cellID) * ThermExpanCoeff.GetCellVar(cellID) * Gravity.GetCellVar(cellID);
            }
        }
        protected class BoussinesqSourceVars_Const : ICellVarArr<Vector3d>
        {
            public double[] ScalarT;
            public double RefT;
            public Vector3d Total;
            public Vector3d GetCellVar(int cellID)
            {
                return (RefT - ScalarT[cellID]) * Total;
            }
        }
    }
}
